package steed.netty.server;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.util.ReferenceCountUtil;
import steed.netty.module.BaseMsg;
import steed.netty.module.CommonMsg;
import steed.netty.module.LoginMsg;
import steed.netty.module.MsgType;
import steed.netty.module.PingMsg;
import steed.util.base.BaseUtil;
import steed.util.base.PropertyUtil;
import steed.util.base.StringUtil;

public abstract class BaseNettyHandler<T> extends SimpleChannelInboundHandler<T> {

	protected static NettyEngine getNettyEngine(String key){
		String className = PropertyUtil.getProperties("nettyEngine.properties").getProperty("netty.server."+key);
		if (StringUtil.isStringEmpty(className)) {
			BaseUtil.getLogger().warn("nettyEngine.properties中未找到netty.server."+key+"的消息处理器");
			return null;
		}
		try {
			return (NettyEngine) Class.forName(className).newInstance();
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			BaseUtil.getLogger().info("获取消息处理器出错",e);
		}
		return null;
	}
	
    
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, T msg) throws Exception {
    	try {
    		dealMsg(channelHandlerContext, convert2BaseMsg(msg));
		} catch (Exception e) {
			BaseUtil.getLogger().warn("服务器处理消息出错!",e);
		}
    }
    
    protected abstract BaseMsg convert2BaseMsg(T t);
//    protected abstract Object convertFromBaseMsg(BaseMsg baseMsg);

	protected void dealMsg(ChannelHandlerContext channelHandlerContext, BaseMsg baseMsg) {
		if(MsgType.LOGIN.equals(baseMsg.getType())){
            dealMessageByEngine("login",baseMsg,channelHandlerContext);
        }else{
            if(NettyChannelMap.getClientId(channelHandlerContext.channel()) == null){
                    //说明未登录，或者连接断了，服务器向客户端发起登录请求，让客户端重新登录
                    LoginMsg loginMsg=new LoginMsg();
                    channelHandlerContext.channel().writeAndFlush(loginMsg);
            }else {
            	 switch (baseMsg.getType()){
                 case PING:{
                	 BaseUtil.out("ping---------");
                     PingMsg pingMsg=(PingMsg)baseMsg;
                     PingMsg replyPing=new PingMsg();
                     channelHandlerContext.writeAndFlush(replyPing);
                 }
                 break;
                 
                 case common:{
                     //收到客户端的请求
                	 BaseUtil.getLogger().debug("收到客户端{}消息----->{}",NettyChannelMap.getClientId(channelHandlerContext.channel()),baseMsg.getContent());
					dealMessageByEngine("common."+((CommonMsg)baseMsg).getMsgCode(),baseMsg,channelHandlerContext);
                 }
                 break;
                 
                 default:
                 break;
             }
			}
        }
        ReferenceCountUtil.release(baseMsg);
	}
	
	

    protected void dealMessageByEngine(String key,BaseMsg baseMsg,ChannelHandlerContext channelHandlerContext){
    	NettyEngine nettyEngine = getNettyEngine(key);
		if (nettyEngine!=null) {
			nettyEngine.dealMessage(baseMsg,channelHandlerContext);
		}
    }
}
